package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.*;
import com.kindergarten.service.IClaTeaService;
import com.kindergarten.service.IClassesService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-27
 */
@Controller
@RequestMapping("/cla-tea-api")
public class ClaTeaController {

    @Autowired
    IClassesService classesService;

    @Autowired
    IUserService iUserService;

    @Autowired
    IClaTeaService iClaTeaService;

    /**
     * 01-跳转到模板引擎页面
     * @return
     */
    @GetMapping("topage")
    @Secured(value = {"ROLE_超级管理员","ROLE_教师"})
    public String topage() {
        return "claTea";
    }

    /**
     * 02-数据加载分页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    @Secured(value = {"ROLE_超级管理员","ROLE_教师"})
    public MMGridPageVoBean page(ClaTea claTea) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();

        List<Integer> classesIds=new ArrayList<Integer>();
        List<Classes> ClassesList = classesService.list(new QueryWrapper<Classes>().like("name", claTea.getName()).eq("del", 0));
        for (Classes classes : ClassesList) {
            classesIds.add(classes.getId());
        }
        if(classesIds.size()==0){
            classesIds.add(-1);
        }

        List<String> userIds=new ArrayList<String>();
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).like("user_name", claTea.getUserName()));
        for (User user : userList) {
            userIds.add(user.getUserId());
        }
        if(userIds.size()==0){
            userIds.add("-1");
        }

        QueryWrapper<ClaTea> queryWrapper = new QueryWrapper<ClaTea>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("del",0); //删除不查询

        queryWrapper.in("class_id",classesIds);
        queryWrapper.in("tec_id",userIds);

        IPage<ClaTea> page = iClaTeaService.page(new Page<ClaTea>(claTea.getPage(), claTea.getLimit()), queryWrapper);
        List<ClaTea> records = page.getRecords();
        for (ClaTea record : records) {
            record.setName(classesService.getById(record.getClassId()).getName());
            record.setUserName(iUserService.getById(record.getTecId()).getUserName());
        }
        pageVoBean.setRows(records); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toaddpage")
    @Secured(value = {"ROLE_超级管理员"})
    public String toaddpage(Model model) {
        //班级
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        //家长集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("claObjects",classesList);
        model.addAttribute("userObjects",userList);
        return "claTeaAdd";
    }

    /**
     * 新增
     *
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    @Secured(value = {"ROLE_超级管理员"})
    public DataResults add(ClaTea claTea) {
        return DataResults.success(ResultCode.SUCCESS, iClaTeaService.save(claTea));
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toupdatepage")
    @Secured(value = {"ROLE_超级管理员"})
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        ClaTea claTea = iClaTeaService.getById(id);
        //班级
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        //家长集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("claObjects",classesList);
        model.addAttribute("userObjects",userList);
        model.addAttribute("claTea", claTea);
        return "claTeaUpdate";  //模板引擎页面
    }

    /**
     * 更新学生
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    @Secured(value = {"ROLE_超级管理员"})
    public DataResults update(ClaTea claTea){
        iClaTeaService.updateById(claTea);
        return DataResults.success(ResultCode.SUCCESS);
    }

    /**
     * 删除
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    @Secured(value = {"ROLE_超级管理员"})
    public DataResults delete(@PathVariable("id") Integer id) {
        ClaTea claTea = new ClaTea();
        claTea.setId(id);
        claTea.setDel(1);
        return DataResults.success(ResultCode.SUCCESS, iClaTeaService.updateById(claTea));
    }

}
